<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :before-close="close">
      <el-form ref="form" v-loading="loading" :model="form" label-width="100px" class="demo-ruleForm">
        <el-row>
          <el-table
            ref="multipleTable"
            :data="list"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="55"
              :selectable="handleDisable"
            />
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="left" inline class="demo-table-expand">
                  <div v-for="item in props.row.goods_attr" :key="item.unit_id">
                    <el-form-item label="规格名称">
                      <span>{{ item.sku_name }}</span>
                    </el-form-item>
                    <el-form-item label="规格图片">
                      <el-image
                        v-if="item.spec_img"
                        :src="item.spec_img"
                        style="height: 50px"
                        :preview-src-list="item.spec_img.split(',')"
                      />
                    </el-form-item>
                    <el-form-item label="规格价格">
                      <span>{{ item.sku_price }}</span>
                    </el-form-item>
                  </div>
                </el-form>
              </template>
            </el-table-column>
            <el-table-column
              label="图片"
              width="120"
            >
              <template slot-scope="scope">
                <el-image
                  v-if="scope.row.goods_img.img"
                  :src="scope.row.goods_img.img"
                  style="height: 50px"
                  :preview-src-list="scope.row.goods_img.img.split(',')"
                />
              </template>
            </el-table-column>
            <el-table-column
              prop="goods_name"
              label="商品名称"
            />
            <el-table-column
              prop="trade_type_name"
              label="贸易类型"
              width="120"
            />
            <el-table-column
              prop="origin_name"
              label="产地"
              width="120"
            />
          </el-table>
          <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />

        </el-row>
        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">同步</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRemoteList, goodsSync } from '@/api/externalGoods'

export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      loading: true,
      list: [],
      total: 0,
      queryParams: {
        page: 1,
        limit: 10
      }
    }
  },
  watch: {
    'open': function(newValue, oldValue) {
      if (newValue) {
        this.getList()
      }
    }
  },
  created() {

  },
  methods: {
    close() {
      this.$emit('update:open', false)
    },
    getList() {
      this.loading = true
      getRemoteList(this.queryParams).then(res => {
        // console.log(res)
        if (res.code === 10000) {
          this.list = res.data
          this.total = res.count
          this.loading = false
        }
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleDisable(row, index) {
      // console.log(row)
      if (row.is_exist) {
        return false
      } else {
        return true
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    submitForm() {
      this.$confirm('此操作将只同步当前页面已勾选的选项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        goodsSync({ lists: this.multipleSelection }).then(res => {
          // console.log(res)
          if (res.code === 10000) {
            this.$message({
              message: '同步成功',
              type: 'success'
            })
            this.close()
            this.$emit('refresh')
            this.loading = false
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            })
            this.loading = false
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>

